﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{

    private Transform mUpDownTransform;
    private Transform mZoomTransform;
    private float UpDownAngle = 0;
    private float zoomPos = 0;
    public float RotateSpeed = 5;
    public float ZoomSpeed = 5;



    void Awake()
    {
        mUpDownTransform = this.transform.Find("UpDown");
        mZoomTransform = this.transform.Find("UpDown/Zoom");
    }
    // Use this for initialization
    void Start()
    {

    }

    public Vector3 TargetPosition;
    public Vector3 TargetAngle;
    public Vector3 TargetZoom;
    // Update is called once per frame
    void Update()
    {
        /*鼠标右键拖拽旋转*/
        if (Input.GetMouseButton(1))
        {
            this.transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * RotateSpeed);
            UpDownAngle -= Input.GetAxis("Mouse Y") * RotateSpeed;
            UpDownAngle = Mathf.Clamp(UpDownAngle, 0, 90);

            mUpDownTransform.localEulerAngles = new Vector3(UpDownAngle, 0, 0);
        }


        /*鼠标中轴滚动缩放*/
        zoomPos += Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;
        zoomPos = Mathf.Clamp(zoomPos, -20, 20f);
        TargetZoom = new Vector3(0, 0, zoomPos);
        mZoomTransform.localPosition = Vector3.Lerp(mZoomTransform.localPosition, TargetZoom, Time.deltaTime * 5);


        
       
        /*鼠标屏幕拖动效果*/
        if (Input.GetMouseButton(2))
        {
            Vector3 hv = Camera.main.transform.right;
            Vector3 vv = Camera.main.transform.up;
            TargetPosition = this.transform.position;
            TargetPosition += hv * -Input.GetAxis("Mouse X");
            TargetPosition += vv * -Input.GetAxis("Mouse Y");
        }

        this.transform.position = Vector3.Lerp(this.transform.position, TargetPosition, Time.deltaTime *10);
    }








}
